package thread.java20230105;

/**
 * 两个线程交替打印AB
 */
public class Demo33 {
    static Object locker = new Object();
    public static void main(String[] args) {
        //1. 打印A
        //2. 叫醒t2
        //3. 等待
        Thread t1 = new Thread(()->{
            try {
                synchronized (locker){
                    while (true){
                        System.out.println("A");
                        locker.notify();
                        locker.wait();
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(()->{
            try {
                synchronized (locker){
                    while (true){
                        System.out.println("B");
                        locker.notify();
                        locker.wait();
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        });

        t1.start();
        t2.start();
    }
}
